<template>
  <div class="student-review">
    
    <!-- 搜索栏 -->
    <el-card class="search-card">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="学生姓名">
          <el-input v-model="searchForm.name" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="提交时间">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="当前状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="全部" value="" />
            <el-option label="待审核" value="pending_review" />
            <el-option label="已通过" value="approved" />
            <el-option label="已驳回" value="incomplete" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <!-- 数据表格 -->
    <el-card class="table-card">
      <template #header>
        <div class="table-header">
          <span>留学生简历列表</span>
          <el-button type="primary" @click="handleRefresh">刷新</el-button>
        </div>
      </template>
      
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="name" label="学生姓名" min-width="20%">
          <template #default="scope">
            <el-button link @click="handleViewDetail(scope.row)">
              {{ scope.row.name }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column prop="submitTime" label="提交时间" min-width="25%" />
        <el-table-column prop="nationality" label="国籍" min-width="15%" />
        <el-table-column prop="status" label="当前状态" min-width="15%">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="25%">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleApprove(scope.row)">
              通过
            </el-button>
            <el-button type="danger" size="small" @click="handleReject(scope.row)">
              驳回
            </el-button>
            <el-button type="info" size="small" @click="handleViewDetail(scope.row)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <!-- 详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="留学生简历详情"
      width="60%"
    >
      <div v-if="currentDetail">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="姓名">{{ currentDetail.fullName }}</el-descriptions-item>
          <el-descriptions-item label="国籍">{{ currentDetail.nationality }}</el-descriptions-item>
          <el-descriptions-item label="性别">{{ currentDetail.gender === 'male' ? '男' : '女' }}</el-descriptions-item>
          <el-descriptions-item label="出生日期">{{ new Date(currentDetail.dateOfBirth).toLocaleDateString('zh-CN') }}</el-descriptions-item>
          <el-descriptions-item label="邮箱">{{ currentDetail.email }}</el-descriptions-item>
          <el-descriptions-item label="手机号">{{ currentDetail.phoneNumber }}</el-descriptions-item>
          <el-descriptions-item label="微信/WhatsApp">{{ currentDetail.wechatWhatsapp }}</el-descriptions-item>
          <el-descriptions-item label="简历状态">{{ getStatusText(currentDetail.resumeStatus) }}</el-descriptions-item>
          <el-descriptions-item label="更新时间">{{ new Date(currentDetail.lastUpdateTime).toLocaleString('zh-CN') }}</el-descriptions-item>
        </el-descriptions>
        
        <!-- 教育经历 -->
        <div class="detail-section" v-if="currentDetail.educationList && currentDetail.educationList.length > 0">
          <h4>教育经历</h4>
          <el-table :data="currentDetail.educationList" style="width: 100%">
            <el-table-column prop="major" label="专业" />
            <el-table-column prop="degree" label="学位" />
          </el-table>
        </div>
        
        <!-- 语言能力 -->
        <div class="detail-section" v-if="currentDetail.languageList && currentDetail.languageList.length > 0">
          <h4>语言能力</h4>
          <el-tag 
            v-for="language in currentDetail.languageList" 
            :key="language.languageId"
            style="margin-right: 8px; margin-bottom: 8px;"
          >
            {{ language.languageName }}
          </el-tag>
        </div>
        
        <!-- 技能特长 -->
        <div class="detail-section" v-if="currentDetail.skillList && currentDetail.skillList.length > 0">
          <h4>技能特长</h4>
          <el-tag 
            v-for="skill in currentDetail.skillList" 
            :key="skill.skillId"
            type="success"
            style="margin-right: 8px; margin-bottom: 8px;"
          >
            {{ skill.skillName }}
          </el-tag>
        </div>
        
        <!-- 驳回理由 -->
        <div class="detail-section" v-if="currentDetail.rejectionReason">
          <h4>驳回理由</h4>
          <p style="color: #f56c6c;">{{ currentDetail.rejectionReason }}</p>
        </div>
      </div>
    </el-dialog>
    
    <!-- 驳回对话框 -->
    <el-dialog
      v-model="rejectDialogVisible"
      title="驳回申请"
      width="40%"
    >
      <el-form :model="rejectForm" label-width="80px">
        <el-form-item label="驳回理由" required>
          <el-input
            v-model="rejectForm.reason"
            type="textarea"
            :rows="4"
            placeholder="请输入驳回理由"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="rejectDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmReject">确认驳回</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getStudentList, reviewStudentResume, getStudentDetail, approveStudentReview, rejectStudentReview } from '@/api/studentReview'

const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const detailDialogVisible = ref(false)
const rejectDialogVisible = ref(false)
const currentDetail = ref(null)

const searchForm = reactive({
  name: '',
  dateRange: [],
  status: ''
})

const rejectForm = reactive({
  reason: ''
})

const tableData = ref([])

// 状态映射
const getStatusType = (status) => {
  const statusMap = {
    pending_review: 'warning',
    approved: 'success',
    incomplete: 'danger'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    pending_review: '待审核',
    approved: '已通过',
    incomplete: '已驳回'
  }
  return statusMap[status] || '未知'
}

// 获取学生列表
const fetchStudentList = async () => {
  try {
    loading.value = true
    const response = await getStudentList()
    
    tableData.value = response.data.map(item => ({
      id: item.studentId,
      name: item.fullName,
      submitTime: new Date(item.submitTime).toLocaleString('zh-CN'),
      nationality: item.nationality,
      status: item.resumeStatus,
      rejectionReason: item.rejectionReason
    }))
    
    total.value = response.total
    currentPage.value = response.page
    pageSize.value = response.size
  } catch (error) {
    ElMessage.error('获取学生列表失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  // 由于后端接口不支持搜索参数，这里暂时只刷新数据
  fetchStudentList()
}

const handleReset = () => {
  searchForm.name = ''
  searchForm.dateRange = []
  searchForm.status = ''
  fetchStudentList()
}

const handleRefresh = () => {
  fetchStudentList()
}

const handleViewDetail = async (row) => {
  try {
    const response = await getStudentDetail(row.id)
    currentDetail.value = response
    detailDialogVisible.value = true
  } catch (error) {
    ElMessage.error('获取学生详情失败')
  }
}

const handleApprove = (row) => {
  ElMessageBox.confirm(
    '确定要通过该学生的申请吗？',
    '确认操作',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
    try {
      const response = await approveStudentReview(row.id)
      if (response.success === true) {
        ElMessage.success(response.message || '申请已通过')
        fetchStudentList() // 刷新列表
      } else {
        ElMessage.error(response.message || '操作失败')
      }
    } catch (error) {
      ElMessage.error('操作失败，请重试')
    }
  })
}

const handleReject = (row) => {
  currentDetail.value = row
  rejectForm.reason = ''
  rejectDialogVisible.value = true
}

const handleConfirmReject = async () => {
  if (!rejectForm.reason.trim()) {
    ElMessage.warning('请输入驳回理由')
    return
  }
  
  try {
    const response = await rejectStudentReview(currentDetail.value.id, rejectForm.reason)
    if (response.success === true) {
      ElMessage.success(response.message || '申请已驳回')
      rejectDialogVisible.value = false
      fetchStudentList() // 刷新列表
    } else {
      ElMessage.error(response.message || '操作失败')
    }
  } catch (error) {
    ElMessage.error('操作失败，请重试')
  }
}

const handleSizeChange = (val) => {
  pageSize.value = val
  fetchStudentList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchStudentList()
}

onMounted(() => {
  fetchStudentList()
})
</script>

<style scoped>
.student-review {
  padding: 20px;
  width: 100%;
  min-height: 100vh;
  box-sizing: border-box;
  background-color: #f5f5f5;
}

.student-review h2 {
  margin-bottom: 20px;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.search-card {
  margin-bottom: 20px;
  width: 100%;
  box-sizing: border-box;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  align-items: center;
}

.search-form .el-form-item {
  margin-bottom: 0;
  margin-right: 0;
}

.table-card {
  margin-bottom: 20px;
  width: 100%;
  box-sizing: border-box;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  font-size: 16px;
}

/* 表格样式优化 */
:deep(.el-table) {
  width: 100% !important;
  font-size: 14px;
}

:deep(.el-table th) {
  background-color: #fafafa;
  font-weight: 600;
  color: #333;
}

:deep(.el-table td) {
  padding: 12px 0;
}

:deep(.el-table .cell) {
  padding: 0 12px;
}

/* 表格列宽度优化 */
:deep(.el-table .el-table__column) {
  min-width: 120px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
  padding: 10px 0;
}

.detail-section {
  margin-top: 20px;
}

.detail-section h4 {
  margin-bottom: 10px;
  color: #333;
  font-weight: 600;
}

.detail-section p {
  color: #666;
  line-height: 1.6;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .student-review {
    padding: 10px;
  }
  
  .search-form {
    flex-direction: column;
    align-items: stretch;
  }
  
  .search-form .el-form-item {
    width: 100%;
  }
}
</style> 